package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_gc.domain.bo.GcjxljjtbgBsInfoBo;
import com.ruoyi.report_sj.domain.ReportSjkyqdBaDetail;
import com.ruoyi.report_sj.mapper.ReportSjkyqdBaDetailMapper;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjSjkyqdbgBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjSjkyqdbgBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjSjkyqdbgBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjSjkyqdbgBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjSjkyqdbgBsInfoService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 砂浆抗压强度试验检测报告Service业务层处理
 *
 * @author zxs
 * @date 2024-07-09
 */
@RequiredArgsConstructor
@Service
public class ReportSjSjkyqdbgBsInfoServiceImpl implements IReportSjSjkyqdbgBsInfoService {

    private final ReportSjSjkyqdbgBsInfoMapper baseMapper;
    private final ReportSjkyqdBaDetailMapper detailMapper;

    /**
     * 查询砂浆抗压强度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportSjSjkyqdbgBsInfoVo queryById(Long excelId){
        ReportSjSjkyqdbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        List<ReportSjkyqdBaDetail> detailList = detailMapper.selectListByBgExcelId(excelId);
        vo.setDetailList(detailList);
        return vo;
    }

    /**
     * 查询砂浆抗压强度试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportSjSjkyqdbgBsInfoVo> queryPageList(ReportSjSjkyqdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjSjkyqdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjSjkyqdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆抗压强度试验检测报告列表
     */
    @Override
    public List<ReportSjSjkyqdbgBsInfoVo> queryList(ReportSjSjkyqdbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjSjkyqdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjSjkyqdbgBsInfo> buildQueryWrapper(ReportSjSjkyqdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjSjkyqdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjSjkyqdbgBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjSjkyqdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSjSjkyqdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), ReportSjSjkyqdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSjSjkyqdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSjSjkyqdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSjSjkyqdbgBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSjSjkyqdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSjSjkyqdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSjSjkyqdbgBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSjSjkyqdbgBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyjcrq() != null, ReportSjSjkyqdbgBsInfo::getShiyjcrq, bo.getShiyjcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSjSjkyqdbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSjSjkyqdbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSjSjkyqdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSjSjkyqdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSjSjkyqdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSjSjkyqdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSjSjkyqdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSjSjkyqdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSjSjkyqdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSjSjkyqdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getLingq()), ReportSjSjkyqdbgBsInfo::getLingq, bo.getLingq());
        lqw.eq(StringUtils.isNotBlank(bo.getShejqddj()), ReportSjSjkyqdbgBsInfo::getShejqddj, bo.getShejqddj());
        lqw.eq(StringUtils.isNotBlank(bo.getShijcc()), ReportSjSjkyqdbgBsInfo::getShijcc, bo.getShijcc());
        lqw.eq(StringUtils.isNotBlank(bo.getChoud()), ReportSjSjkyqdbgBsInfo::getChoud, bo.getChoud());
        lqw.eq(StringUtils.isNotBlank(bo.getBanhfs()), ReportSjSjkyqdbgBsInfo::getBanhfs, bo.getBanhfs());
        lqw.eq(StringUtils.isNotBlank(bo.getYanghfs()), ReportSjSjkyqdbgBsInfo::getYanghfs, bo.getYanghfs());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSjSjkyqdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportSjSjkyqdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportSjSjkyqdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportSjSjkyqdbgBsInfo::getBgFlag, bo.getBgFlag());
        return lqw;
    }

    /**
     * 新增砂浆抗压强度试验检测报告
     */
    @Override
    public Boolean insertByBo(ReportSjSjkyqdbgBsInfoBo bo) {
        ReportSjSjkyqdbgBsInfo add = BeanUtil.toBean(bo, ReportSjSjkyqdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆抗压强度试验检测报告
     */
    @Override
    public Boolean updateByBo(ReportSjSjkyqdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportSjSjkyqdbgBsInfo update = BeanUtil.toBean(bo, ReportSjSjkyqdbgBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    private void fillExcelResult(ReportSjSjkyqdbgBsInfoBo bo) {
//        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
//            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos()) {
//                if ("不符合".equals(detailVo.getJiegpd())) {
//                    bo.setExcelResult(2);
//                    return;
//                }
//            }
//            bo.setExcelResult(1);
//        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjSjkyqdbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆抗压强度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
